<?php

$this->LoadHelper('ApiHelper');
$ApiHelper = new ApiHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$check = $ApiHelper->tokenCheck($this->Param);
if (!$check['success']) ErrorMsg::ApiMsg($check['msg'], $check['code']);

if (empty($this->Param['option'])) {
    
} else {
    switch ($this->Param['option']) {
        case 'list':            
            $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
            $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
            $this->LoadResurces('oldapi/class.carloan');
            $carloan = new Carloan();
            $member_info = $MemberHelper->getMember($this->Param['uid']);
            switch($member_info['u_scope']){
                case '用户级':
                    $dpcode = '-1';
                    break;
                case '部门级':
                    $dpcode = $member_info['u_dpcode'];
                    break;
                default :
                    $dpcode = '';
            }
            $result = $carloan->getPayorder($dpcode, $page, $rp);
            $result = json_decode($result, TRUE);
            
            $json_data = array('total' => $result['total'], 'rows' => array(), 'pagetotal' => ceil($result['total'] / $rp));
            if (!empty($result['root'])){
                foreach ($result['root'] AS $rowNum => $row) {
                    $json_data['rows'][] = $row;
                }
            }
            $ApiHelper->logSave('waitpay_list', $this->Param);
            ErrorMsg::ApiMsg('请求成功', 0, $json_data);
            break;
        case 'dopay':
            $pd_id = empty($this->Param['pd_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['pd_id'];
            $this->LoadResurces('oldapi/class.carloan');
            $carloan = new Carloan();
            $result = $carloan->doPay($pd_id);
            $result = json_decode($result, TRUE);
            if($result['success']){
                ErrorMsg::ApiMsg('支付成功', 0);
            }else{
                ErrorMsg::ApiMsg('支付失败'.$result['msg']);
            }
            
            break;
        default :
    }
}